// pages/mine/mySize/newSize/newSize.js
const app = getApp();
const util = require("../../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    customOptionList: [],
    partIndex: "",
    targetIndex: "",
    bodyFeature: [{
      id: 0,
      text: "背部厚实",
      isChecked: true
    },
    {
      id: 1,
      text: "溜肩严重",
      isChecked: false
    },
    {
      id: 2,
      text: "手臂特长",
      isChecked: false
    },
    {
      id: 3,
      text: "肩膀特别宽",
      isChecked: false
    },
    {
      id: 4,
      text: "胸部肌肉特别多",
      isChecked: false
    },
    {
      id: 5,
      text: "脖子短",
      isChecked: false
    }
    ],
    legend: [{
      id: 0,
      img: "http://jiangping.goobye.cn/zhengmian.png",
      text: "正面照"
    },
    {
      id: 1,
      img: "http://jiangping.goobye.cn/cemian.png",
      text: "侧面照"
    },
    {
      id: 2,
      img: "http://jiangping.goobye.cn/beimian.png",
      text: "背面照"
    }
    ],
    customList: [],
    files: [],
    name: [],
    height: [],
    age: [],
    weight: [],
    feature: [],
    note: [],
    img: [],
    pics: [],
    // 获取姓名
    sname: "",
    sheight: "",
    sage: "",
    sweight: "",
    snote: "",
    sizeId: ""
  },

  // 选择对应体型
  selectPart: function (e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      partIndex: index
    })
  },

  // 选择对应目标
  selectPartStyle: function (e) {
    let that = this
    let index = e.currentTarget.dataset.index
    that.setData({
      targetIndex: index
    })
    for (let i = 0; i < that.data.customOptionList[that.data.partIndex].paramOptions.length; i++) {
      if (that.data.targetIndex == i) {
        that.data.customOptionList[that.data.partIndex].paramOptions[i].select = false
      } else {
        that.data.customOptionList[that.data.partIndex].paramOptions[i].select = true
      }
      this.setData(this.data)
    }
  },

  // 选择身体特征
  selectFeature: function (e) {
    let that = this
    let index = e.currentTarget.dataset.index
    let item = that.data.bodyFeature[index]
    item.isChecked = !item.isChecked
    that.setData({
      bodyFeature: that.data.bodyFeature
    });
    let arr = []
    for (let i = 0; i < that.data.bodyFeature.length; i++) {
      if (that.data.bodyFeature[i].isChecked == true) {
        let text = that.data.bodyFeature[i].text
        arr.push(text)
      }
    }
    let feature = {
      paramName: "身体特征",
      option: arr
    }
    that.data.feature.push(feature)
    that.setData(that.data)
    // console.log(that.data.feature)
  },

  // 上传图片
  chooseImage: function (e) {
    var that = this,
      pics = that.data.pics
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        // console.log(res)
        that.setData({
          pics: res.tempFilePaths
        })
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
        // console.log(that.data.files)
        let img = {
          paramName: "上传图片",
          option: that.data.files
        }
        that.data.img.push(img)
        that.setData(that.data)
        // console.log(that.data.img)
        let imgsrc = res.tempFilePaths
        pics = pics.concat(imgsrc)
      }
    })
  },
  //删除图片
  deleteImg: function (res) {
    var files = this.data.files
    for (var i = 0; i < files.length; i++) {
      if (i == res.currentTarget.dataset.id) {
        files.splice(i, 1)
        break
      }

    }
    this.setData({
      files: files
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },


  //多张图片上传
  uploadimg: function (data) {
    var that = this,
      i = data.i ? data.i : 0, //当前上传的哪张图片
      success = data.success ? data.success : 0, //上传成功的个数
      fail = data.fail ? data.fail : 0; //上传失败的个数
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'file', //这里根据自己的实际情况改
      formData: data.data, //这里是上传图片时一起上传的数据
      success: (resp) => {
        success++; //图片上传成功，图片上传成功的变量+1
        // console.log(resp)
        // console.log(i);
        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1
      },
      fail: (res) => {
        fail++; //图片上传失败，图片上传失败的变量+1
        // console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        // console.log(i);
        i++; //这个图片执行完上传后，开始上传下一张
        if (i == data.path.length) { //当图片传完时，停止调用          
          // console.log('执行完毕');
          // console.log('成功：' + success + " 失败：" + fail);
        } else { //若图片还没有传完，则继续调用函数
          // console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }
      }
    });
  },

  // 获取姓名
  nameInput: function (e) {
    let that = this
    let name = {
      paramName: "姓名",
      option: e.detail.value
    }
    that.data.name.push(name)
    that.data.sname = e.detail.value
    that.setData(that.data)
    // console.log(that.data.name)
  },

  // 获取身高
  heightInput: function (e) {
    let that = this
    let height = {
      paramName: "身高",
      option: e.detail.value
    }
    that.data.height.push(height)
    that.data.sheight = e.detail.value
    that.setData(that.data)
    // console.log(that.data.height)
  },

  // 获取年龄
  ageInput: function (e) {
    let that = this
    let age = {
      paramName: "年龄",
      option: e.detail.value
    }
    that.data.age.push(age)
    that.data.sage = e.detail.value
    that.setData(that.data)
    // console.log(that.data.age)
  },

  // 获取体重
  weightInput: function (e) {
    let that = this
    let weight = {
      paramName: "体重",
      option: e.detail.value
    }
    that.data.weight.push(weight)
    that.data.sweight = e.detail.value
    that.setData(that.data)
    // console.log(that.data.weight)
  },

  // 获取身体特征备注
  featureNote: function (e) {
    let that = this
    let note = {
      paramName: "身体特征备注",
      option: e.detail.value
    }
    that.data.note.push(note)
    that.data.snote = e.detail.value
    that.setData(that.data)
    // console.log(that.data.note)
  },

  // 点击确认修改
  finishBtn: function (e) {
    let that = this
    // 获取用户选中的体型元素
    let list = that.data.customOptionList
    for (let i = 0; i < list.length; i++) {
      for (let j = 0; j < list[i].paramOptions.length; j++) {
        if (list[i].paramOptions[j].select == false) {
          let custom = {
            paramName: list[i].paramName,
            option: list[i].paramOptions[j].option
          }
          that.data.customList.push(custom)
        }
      }
    }
    
    let last = that.data
    // sname: "",sheight: "",sage: "",sweight: "",snote: ""
    if (last.sname) {
      let name = {
        paramName: "姓名",
        option: last.sname
      }
      that.data.name.push(name)
      that.setData(that.data)
    } 
    if (last.sheight) {
      let height = {
        paramName: "身高",
        option: last.sheight
      }
      that.data.height.push(height)
      that.setData(that.data)
    } 
    if (last.sage) {
      let age = {
        paramName: "年龄",
        option: last.sage
      }
      that.data.age.push(age)
      that.setData(that.data)
    } 
    if (last.sweight) {
      let weight = {
        paramName: "体重",
        option: last.sweight
      }
      that.data.weight.push(weight)
      that.setData(that.data)
    } 
    if (last.snote) {
      let note = {
        paramName: "身体特征备注",
        option: last.snote
      }
      that.data.note.push(note)
      that.setData(that.data)
    }
    if (last.bodyFeature) {
      let arr = []
      for (let i = 0; i < that.data.bodyFeature.length; i++) {
        if (that.data.bodyFeature[i].isChecked == true) {
          let text = that.data.bodyFeature[i].text
          arr.push(text)
        }
      }
      let feature = {
        paramName: "身体特征",
        option: arr
      }
      that.data.feature.push(feature)
      that.setData(that.data)
    }
    last.name = last.name[last.name.length - 1]
    last.height = last.height[last.height.length - 1]
    last.age = last.age[last.age.length - 1]
    last.weight = last.weight[last.weight.length - 1]
    last.feature = last.feature[last.feature.length - 1]
    last.note = last.note[last.note.length - 1]
    // last.img = last.img[last.img.length - 1]
    last.customList = last.customList.concat(last.name, last.height, last.age, last.weight, last.feature, last.note)
    that.setData(that.data)
    console.log(last.customList)


    // 将尺寸信息传回后台
    let msgUrl = app.globalData.baseUrl + "weixin/editSize"
    let data = {
      cid: wx.getStorageSync('cid'),
      msg: that.data.customList,
      sid: that.data.sizeId
    }
    wx.showLoading({
      title: '正在修改',
    })
    util.requestLoading(msgUrl, data, "GET").then(res => {
      console.log(res)
      // 上传图片
      let pics = that.data.pics
      // console.log(pics)
      if (pics.length == 0) {
        wx.hideLoading()
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.hideLoading()
        that.uploadimg({
          url: app.globalData.baseUrl + 'weixin/addSizePics',
          path: pics,
          data: {
            id: res.data.id
          }
        })
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    // 请求尺寸参数
    let sizeUrl = app.globalData.baseUrl + "weixin/clickEditSize"
    let sizeId = options.sizeId
    let data = {
      sizeId: sizeId
    }
    that.setData({
      sizeId: sizeId
    })
    util.requestLoading(sizeUrl, data, "GET").then(res => {
      console.log(res)
      that.setData({
        customOptionList: res.data.customOptionList,
        bodyFeature: res.data.bodyFeature,
        sname: res.data.sName,
        sheight: res.data.height,
        sage: res.data.age,
        sweight: res.data.weight,
        snote: res.data.note
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})